﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.IRepository;
using FineAdmin.Common;
using Dapper;
using HuiJI.Tools.TBK;
using System.Globalization;

namespace FineAdmin.Service
{
    public class RushService : BaseService<RushModel>, IRushService
    {
        public IOpRecordService OpRecordService { get; set; }
        public IRushRepository RushRepository { get; set; }
        public ISystemParameterRepository SystemParameterRepository { get; set; }

        public IAdZoneRepository AdZoneRepository { get; set; }

        public IEnumerable<RushModel> GetRushList()
        {
            return RushRepository.GetRushList();
        }


        public dynamic GetListByFilter(RushModel filter, PageInfo pageInfo)
        {
            string _where = " where 1=1";
            pageInfo.field = "StartTime";
            pageInfo.order = "ASC";
            _where = CreateTimeWhereStr(filter.StartEndDate, _where, null, "StartTime");
            return GetListByFilter(filter, pageInfo, _where);
        }

        public int CreateData()
        {
           

            string Api_Url = SystemParameterRepository.GetByCode("TBK_API").SPValue;
            var adzList = AdZoneRepository.GetAdZoneList();
            var adzDefault = adzList.FirstOrDefault(m => m.IsDefault == true && m.Type == 1);
            TBKTools tbk_tools = new TBKTools(Api_Url);
            DateTime now = DateTime.Now;
            //DateTime todayStart = new DateTime(now.Year, now.Month, now.Day);//今日开始
            //DateTime tomorrowEnd = todayStart.AddDays(2).AddSeconds(-1); ;//明日结束
            long page = 1;
            long pageSize = 20;
            //long dataCount = 40;
            int totality = 0;

            //每个时段取20个数据就可以
            DateTime startHout = new DateTime(now.Year, now.Month, now.Day);//今日开始时段
            DateTime nextHour = startHout.AddHours(1).AddSeconds(-1);// 下一段时段


            var rushAll = RushRepository.GetAll();

            //RushRepository.DeleteByWhere("where 1=1");
            for (int i = 0; i < 48; i++)
            {
                var ret = tbk_tools.TaoQiangGou(adzDefault.TBK_AppKey, adzDefault.TBK_AppSecret, adzDefault.TBK_AdzoneId.Value, startHout, nextHour, page, pageSize);
                var ret2 = tbk_tools.TaoQiangGou(adzDefault.TBK_AppKey, adzDefault.TBK_AppSecret, adzDefault.TBK_AdzoneId.Value, startHout, nextHour, page + 1, pageSize);

                OpRecordService.AddOpRecord("淘抢购", "淘宝客API", 2);

                startHout = startHout.AddHours(1);
                nextHour = nextHour.AddHours(1);
                ret.Results.AddRange(ret2.Results);
                foreach (var item in ret.Results)
                {
                    var temprush = rushAll.FirstOrDefault(m => m.ItemID == item.NumIid.ToString() && m.Title == item.Title);
                    if (temprush == null)
                    {

                        RushRepository.Insert(new RushModel()
                        {
                            CategoryName = item.CategoryName,
                            ClickUrl = item.ClickUrl,
                            CreateTime = DateTime.Now,
                            CreateUserId = 1,
                            EndTime = DateTime.Parse(item.EndTime),
                            StartTime = DateTime.Parse(item.StartTime),
                            Title = item.Title,
                            TotalAmount = (int)item.TotalAmount,
                            ZkFinalPrice = item.ZkFinalPrice,
                            ItemID = item.NumIid.ToString(),
                            PicUrl = item.PicUrl,
                            ReservePrice = item.ReservePrice,
                            SoldNum = (int)item.SoldNum,
                            UpdateTime = DateTime.Now,
                            UpdateUserId = 1

                        });

                    }
                    else
                    {
                        temprush.SoldNum = (int)item.SoldNum;
                        temprush.UpdateTime = DateTime.Now;
                        RushRepository.UpdateById(temprush, "SoldNum");
                    }
                }
                totality += (int)ret.TotalResults;
            }

            //while (dataCount == 40)
            //{
            //    //获取两天的所有数据

            //    var ret = tbk_tools.TaoQiangGou(adzDefault.TBK_AppKey, adzDefault.TBK_AppSecret, adzDefault.TBK_AdzoneId.Value, todayStart, tomorrowEnd, page, pageSize);

            //    //加入数据库

            //    foreach (var item in ret.Results)
            //    {
            //        RushRepository.Insert(new RushModel()
            //        {
            //            CategoryName = item.CategoryName,
            //            ClickUrl = item.ClickUrl,
            //            CreateTime = DateTime.Now,
            //            CreateUserId = 1,
            //            EndTime = DateTime.Parse(item.EndTime),
            //            StartTime = DateTime.Parse(item.StartTime),
            //            Title = item.Title,
            //            TotalAmount = (int)item.TotalAmount,
            //            ZkFinalPrice = item.ZkFinalPrice,
            //            ItemID = item.NumIid.ToString(),
            //            PicUrl = item.PicUrl,
            //            ReservePrice = item.ReservePrice,
            //            SoldNum = (int)item.SoldNum,
            //            UpdateTime = DateTime.Now,
            //            UpdateUserId = 1


            //        });
            //    }
            //    page += 1;
            //    totality += (int)dataCount;
            //    dataCount = ret.TotalResults;
            //}
            return totality;
        }

    }

}
